﻿Imports DevExpress.XtraEditors

Public Class frmChietTinh_CapNhat
    Dim Me_AddEdit As String
    Dim Me_Dtb As New DataTable

#Region "Proc"
    Private Sub InitControl()
        Me.txtSo_Don.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.Numeric
        Me.txtSo_Don.Properties.Mask.EditMask = "############################"
        Me.txtSo_Don.Properties.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric

        Me.txtVanChuyen.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
        Me.txtVanChuyen.Properties.DisplayFormat.FormatString = "### ### ###;Null"
    End Sub

    Private Function GetNewSoPhieu() As String
        Dim s As String
        Dim myChietTinhF As New ChietTinhF
        Dim myDtb As New DataTable
        myDtb = myChietTinhF.GetLastSoPhieu
        If myDtb.Rows.Count = 1 Then
            s = (CLng(myDtb.Rows(0).Item("sophieu").ToString.Substring(2, 5)) + 1).ToString
            Dim i As Integer
            For i = 1 To 5 - s.Length
                s = "0" + s
            Next
            Return s
        Else
            Return "00001"
        End If

    End Function

    Private Sub AddNew()
        Grid_LoadId("")
        Status(1)
        Me.lueLoaiChietTinh.EditValue = ""
        Me.txtSoPhieu1.Text = Project_Nam.ToString.Substring(2, 2)
        Me.txtSoPhieu2.Text = GetNewSoPhieu()
        Me.txtNgayLap.EditValue = Now.Date
        Me.txtSo_Don.EditValue = ""
        Me.lueMa_DViDChinh.EditValue = ""
        Me.txtKhachHang.EditValue = ""
        Me.txtVanChuyen.EditValue = 0
        Me_AddEdit = "add"

    End Sub

    Private Sub LoadEdit()
        Dim myChietTinhF As New ChietTinhF
        Dim Dtb As New DataTable
        Dtb = myChietTinhF.GetWhereSoPhieu(frmChietTinh_CapNhat_SoPhieu)
        If Dtb.Rows.Count = 1 Then
            Me.lueLoaiChietTinh.EditValue = Dtb.Rows(0).Item("maloaichiettinh")
            Me.txtSoPhieu1.Text = Dtb.Rows(0).Item("sophieu").ToString.Substring(0, 2)
            Me.txtSoPhieu2.Text = Dtb.Rows(0).Item("sophieu").ToString.Substring(2, 5)
            Me.txtNgayLap.Text = Dtb.Rows(0).Item("ngaylap")
            Me.txtKhachHang.Text = Dtb.Rows(0).Item("khachhang")
            Me.txtDiaChi.Text = Dtb.Rows(0).Item("diachi")
            Me.txtVanChuyen.Value = Dtb.Rows(0).Item("vanchuyen")
        End If

        Grid_LoadId(frmChietTinh_CapNhat_SoPhieu)
        Me.txtSoMon.EditValue = CountSoMon()
        Status(0)

        Me_AddEdit = "edit"
    End Sub

    Private Sub InitLookup()

        Me.lueLoaiChietTinh.Properties.Columns.AddRange(New DevExpress.XtraEditors.Controls.LookUpColumnInfo() {New DevExpress.XtraEditors.Controls.LookUpColumnInfo("maloaichiettinh", "Mã", 20, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Center), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("tenloaichiettinh", "Tên loại", 80, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Default), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("loaichiettinh", "Loại chiết tính", 85, DevExpress.Utils.FormatType.None, "", False, DevExpress.Utils.HorzAlignment.Center)})
        Me.lueLoaiChietTinh.Properties.DisplayMember = "loaichiettinh"
        Me.lueLoaiChietTinh.Properties.ValueMember = "maloaichiettinh"

        Dim myLoaiChietTinhF As New LoaiChietTinhF
        Dim myDtb As New DataTable

        myDtb = myLoaiChietTinhF.GetAll
        Me.lueLoaiChietTinh.Properties.DataSource = myDtb


        Me.lueMa_DViDChinh.Properties.Columns.AddRange(New DevExpress.XtraEditors.Controls.LookUpColumnInfo() {New DevExpress.XtraEditors.Controls.LookUpColumnInfo("MA_DVIDCHINH", "Mã địa chính", 80, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Center), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("TEN_DVIDCHINH", "Phường/Xã", 80, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Default), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("TEN_HUYEN", "Huyện/TP", 80, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Default), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("TEN", "Tên địa chính", 80, DevExpress.Utils.FormatType.None, "", False, DevExpress.Utils.HorzAlignment.Default)})
        Me.lueMa_DViDChinh.Properties.DisplayMember = "TEN"
        Me.lueMa_DViDChinh.Properties.ValueMember = "MA_DVIDCHINH"
        Me.lueMa_DViDChinh.Properties.NullText = ""

        Dim myDiaChinhF As New DiaChinhF

        myDtb = myDiaChinhF.GetAll
        Me.lueMa_DViDChinh.Properties.DataSource = myDtb

    End Sub

    Private Sub Grid_Init()

        Dim i As Integer
        With Me.GridView1
            i = 0
            .Columns(i).FieldName = "tenhangmuc"
            .Columns(i).Caption = "Hạng mục"
            .Columns(i).Visible = False

            i = 1
            .Columns(i).FieldName = "ma"
            .Columns(i).Visible = False

            i = 2
            .Columns(i).FieldName = "ten"
            .Columns(i).Caption = "Danh mục"
            .Columns(i).Width = 200

            i = 3
            .Columns(i).FieldName = "dvt"
            .Columns(i).Caption = "Đvt"
            .Columns(i).Width = 40

            i = 4
            .Columns(i).FieldName = "sldl"
            .Columns(i).Caption = "Điện lực"
            .Columns(i).Width = 65
            .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            .Columns(i).DisplayFormat.FormatString = "### ###.##;Null"

            i = 5
            .Columns(i).FieldName = "slkh"
            .Columns(i).Caption = "Khách hàng"
            .Columns(i).Width = 65
            .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            .Columns(i).DisplayFormat.FormatString = "### ###.##;Null"

            i = 6
            .Columns(i).FieldName = "dgvl"
            .Columns(i).Caption = "Vật liệu"
            .Columns(i).Width = 70
            .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            .Columns(i).DisplayFormat.FormatString = "### ###.###;Null"

            i = 7
            .Columns(i).FieldName = "dgnc"
            .Columns(i).Caption = "N công"
            .Columns(i).Width = 70
            .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            .Columns(i).DisplayFormat.FormatString = "### ###.###;Null"

            i = 8
            .Columns(i).FieldName = "dgmtc"
            .Columns(i).Caption = "Máy TC"
            .Columns(i).Width = 70
            .Columns(i).DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric
            .Columns(i).DisplayFormat.FormatString = "### ###.###;Null"

            Dim j As Integer
            For j = 0 To .Columns.Count - 1
                .Columns(j).OptionsColumn.AllowEdit = False
            Next

            .Columns(0).Group()

        End With


    End Sub

    Private Sub Grid_LoadNew(ByVal MaLoaiChietTinh As String)
        Dim myChietTinhCtF As New ChietTinhCtF

        Me_Dtb = myChietTinhCtF.GetNew(MaLoaiChietTinh)
        Me.GridControl1.DataSource = Me_Dtb

        Me.GridView1.ExpandAllGroups()
    End Sub

    Private Sub Grid_LoadId(ByVal SoPhieu As String)
        Dim myChietTinhCtF As New ChietTinhCtF

        Me_Dtb = myChietTinhCtF.GetWhereSophieu(SoPhieu)
        Me.GridControl1.DataSource = Me_Dtb

        Me.GridView1.ExpandAllGroups()
    End Sub

    Private Sub Status(ByVal i As Integer)
        Select Case i
            Case 0 'xem
                Me.lueLoaiChietTinh.Enabled = False
                Me.txtSoPhieu1.Enabled = False
                Me.txtSoPhieu2.Enabled = False
                Me.txtNgayLap.Enabled = False
                Me.txtSo_Don.Enabled = False
                Me.lueMa_DViDChinh.Enabled = False
                Me.txtKhachHang.Enabled = False
                Me.txtDiaChi.Enabled = False
                Me.txtVanChuyen.Enabled = False

                Me.cmdThemChietTinh.Text = "T&hêm chiết tính"
                Me.cmdSuaChietTinh.Text = "&Sửa"
                Me.cmdXoaChietTinh.Enabled = True
                Me.btnXem.Enabled = True
                Me.cmdThemVatTu.Enabled = False
                Me.cmdXoaVatTu.Enabled = False

                Me.GridView1.Columns(4).OptionsColumn.AllowEdit = False 'sldl
                Me.GridView1.Columns(5).OptionsColumn.AllowEdit = False 'slkh

            Case 1 'them
                Me.lueLoaiChietTinh.Enabled = True
                Me.txtSoPhieu1.Enabled = False
                Me.txtSoPhieu2.Enabled = True
                Me.txtNgayLap.Enabled = True
                Me.txtSo_Don.Enabled = True
                Me.lueMa_DViDChinh.Enabled = True
                Me.txtKhachHang.Enabled = True
                Me.txtDiaChi.Enabled = True
                Me.txtVanChuyen.Enabled = True

                Me.cmdThemChietTinh.Text = "&Ghi chiết tính"
                Me.cmdSuaChietTinh.Text = "&Bỏ qua"
                Me.cmdXoaChietTinh.Enabled = False
                Me.btnXem.Enabled = False
                Me.cmdThemVatTu.Enabled = True
                Me.cmdXoaVatTu.Enabled = True

                Me.GridView1.Columns(4).OptionsColumn.AllowEdit = True 'sldl
                Me.GridView1.Columns(5).OptionsColumn.AllowEdit = True 'slkh

            Case 2 'sua
                Me.lueLoaiChietTinh.Enabled = False
                Me.txtSoPhieu1.Enabled = False
                Me.txtSoPhieu2.Enabled = False
                Me.txtNgayLap.Enabled = True
                Me.txtSo_Don.Enabled = True
                Me.lueMa_DViDChinh.Enabled = True
                Me.txtKhachHang.Enabled = True
                Me.txtDiaChi.Enabled = True
                Me.txtVanChuyen.Enabled = True

                Me.cmdThemChietTinh.Text = "&Ghi chiết tính"
                Me.cmdSuaChietTinh.Text = "&Bỏ qua"
                Me.cmdXoaChietTinh.Enabled = False
                Me.btnXem.Enabled = False
                Me.cmdThemVatTu.Enabled = True
                Me.cmdXoaVatTu.Enabled = True

                Me.GridView1.Columns(4).OptionsColumn.AllowEdit = True 'sldl
                Me.GridView1.Columns(5).OptionsColumn.AllowEdit = True 'slkh

        End Select
    End Sub

    Private Sub AddDetail()
        'dua danh sach ma trong grid vao _dtb
        frmChietTinh_CapNhat_Dtb = New System.Data.DataTable("MyDTB")
        frmChietTinh_CapNhat_Dtb.Columns.Add("ma", GetType(String))
        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            frmChietTinh_CapNhat_Dtb.Rows.Add(New Object() {Me.GridView1.GetDataRow(i).Item("ma")})
        Next

        'mo form tra cuu
        Project_Form = Me.Name
        Dim f As New frmDanhMuc_TraCuu
        f.ShowDialog()

        'them vat tu
        Dim myDanhMucF As New DanhMucF
        Dim myDtb As New DataTable
        For i = 0 To frmDanhMuc_TraCuu_Dtb.Rows.Count - 1
            myDtb = myDanhMucF.GetWhereMa(frmDanhMuc_TraCuu_Dtb.Rows(i).Item("ma"))
            If myDtb.Rows.Count = 0 Then Exit Sub

            Me_Dtb.Rows.Add()
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("tenhangmuc") = myDtb.Rows(0).Item("tenhangmuc")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("ma") = myDtb.Rows(0).Item("ma")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("ten") = myDtb.Rows(0).Item("ten")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("dvt") = myDtb.Rows(0).Item("dvt")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("sldl") = myDtb.Rows(0).Item("sldl")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("slkh") = myDtb.Rows(0).Item("slkh")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("dgvl") = myDtb.Rows(0).Item("dgvl")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("dgnc") = myDtb.Rows(0).Item("dgnc")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("dgmtc") = myDtb.Rows(0).Item("dgmtc")

        Next

    End Sub

    Private Function CheckKey() As Boolean
        Dim myChietTinhF As New ChietTinhF
        If myChietTinhF.ExistsSoPhieu(Me.txtSoPhieu1.Text.Trim + Me.txtSoPhieu2.Text.Trim) Then
            XtraMessageBox.Show("Số phiếu đã tồn tại", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txtSoPhieu2.Focus()
            Return False
            Exit Function
        End If
        Return True
    End Function

    Private Function CheckInput() As Boolean
        'loai chiet tinh
        If Me.lueLoaiChietTinh.EditValue = "" Then
            XtraMessageBox.Show("Loại chiết tính không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.lueLoaiChietTinh.Focus()
            Return False
            Exit Function
        End If

        'so phieu
        If Me.txtSoPhieu2.EditValue.ToString.Length <> 5 Then
            XtraMessageBox.Show("Số phiếu không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txtSoPhieu2.Focus()
            Return False
            Exit Function
        End If

        'ngay lap
        If Me.txtNgayLap.DateTime.Month <> Project_Thang Or Me.txtNgayLap.DateTime.Year <> Project_Nam Then
            XtraMessageBox.Show("Ngày lập phải nằm trong tháng năm làm việc", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txtNgayLap.Focus()
            Return False
            Exit Function
        End If

        Return True
    End Function

    Private Function SaveMaster() As Boolean
        Dim myChietTinh As New ChietTinh
        Dim myChietTinhF As New ChietTinhF

        myChietTinh.MaLoaiChietTinh = Me.lueLoaiChietTinh.EditValue
        myChietTinh.SoPhieu = Me.txtSoPhieu1.Text.Trim + Me.txtSoPhieu2.Text.Trim
        myChietTinh.NgayLap = Me.txtNgayLap.DateTime.Date
        myChietTinh.KhachHang = Me.txtKhachHang.Text.Trim
        myChietTinh.DiaChi = Me.txtDiaChi.Text.Trim
        myChietTinh.VanChuyen = Me.txtVanChuyen.Value
        myChietTinh.Ma_DViDChinh = Me.lueMa_DViDChinh.EditValue
        myChietTinh.So_Don = Me.txtSo_Don.Text.Trim

        If Not myChietTinhF.Save(myChietTinh) Then
            XtraMessageBox.Show("Ghi tiêu đề chiết tính không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
            Exit Function
        End If

        Return True
    End Function

    Private Function SaveDetail() As Boolean
        Dim myChietTinhF As New ChietTinhF
        Dim myChietTinhCt As New ChietTinhCt
        Dim myChietTinhCtF As New ChietTinhCtF

        If Not myChietTinhCtF.Delete(Me.txtSoPhieu1.Text.Trim + Me.txtSoPhieu2.Text.Trim) Then
            Exit Function
            Return False
        End If

        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            If Me.GridView1.GetDataRow(i).Item("sldl") <> 0 Or Me.GridView1.GetDataRow(i).Item("slkh") <> 0 Then
                myChietTinhCt.SoPhieu = Me.txtSoPhieu1.Text.Trim + Me.txtSoPhieu2.Text.Trim
                myChietTinhCt.Ma = Me.GridView1.GetDataRow(i).Item("ma")
                myChietTinhCt.Sldl = Me.GridView1.GetDataRow(i).Item("sldl")
                myChietTinhCt.Slkh = Me.GridView1.GetDataRow(i).Item("slkh")
                myChietTinhCt.Dgvl = Me.GridView1.GetDataRow(i).Item("dgvl")
                myChietTinhCt.Dgnc = Me.GridView1.GetDataRow(i).Item("dgnc")
                myChietTinhCt.Dgmtc = Me.GridView1.GetDataRow(i).Item("dgmtc")

                If Not myChietTinhCtF.Insert(myChietTinhCt) Then
                    XtraMessageBox.Show("Ghi chi tiết vật tư lỗi tại dòng " + i.ToString, "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    'xoa tieu de
                    If Not myChietTinhF.Delete(Me.txtSoPhieu1.Text.Trim + Me.txtSoPhieu2.Text.Trim) Then
                        XtraMessageBox.Show("Xóa tiêu đề chiết tính không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If

                    Return False
                    Exit Function

                End If
            End If
        Next

        If Not myChietTinhF.ThanhToan(Me.txtSoPhieu1.Text.Trim + Me.txtSoPhieu2.Text.Trim) Then
            XtraMessageBox.Show("Tính chi phí không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
            Exit Function
        End If

        Return True
    End Function

    Private Function CountSoMon() As Decimal
        Dim nSldl, nSlkh As Decimal
        Dim i As Integer
        Dim n As Decimal
        For i = 0 To Me.GridView1.DataRowCount - 1
            nSldl = IIf(Me.GridView1.GetRowCellValue(i, "sldl") Is System.DBNull.Value, 0, Me.GridView1.GetRowCellValue(i, "sldl"))
            nSlkh = IIf(Me.GridView1.GetRowCellValue(i, "slkh") Is System.DBNull.Value, 0, Me.GridView1.GetRowCellValue(i, "slkh"))

            If nSldl <> 0 Or nSlkh <> 0 Then
                n = n + 1
            End If
        Next

        Return n
    End Function

#End Region

    Private Sub frmChietTinh_CapNhat_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Me.lueLoaiChietTinh.Focus()
    End Sub

    Private Sub frmChietTinh_CapNhat_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        frmChietTinh_CapNhat_Exists = False
    End Sub

    Private Sub frmChietTinh_CapNhat_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitLookup()
        InitControl()
        Grid_Init()

        If frmChietTinh_CapNhat_AddEdit = "add" Then
            AddNew()
        ElseIf frmChietTinh_CapNhat_AddEdit = "edit" Then
            LoadEdit()
        End If

    End Sub

    Private Sub lueLoaiChietTinh_EditValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles lueLoaiChietTinh.EditValueChanged
        If Me_AddEdit = "add" Then
            Grid_LoadNew(Me.lueLoaiChietTinh.EditValue)
        End If
    End Sub

    Private Sub cmdThemChietTinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThemChietTinh.Click
        If Me.cmdThemChietTinh.Text = "T&hêm chiết tính" Then 'add new-----------------------------------------------------
            AddNew()
            Me.lueLoaiChietTinh.Focus()

        ElseIf Me.cmdThemChietTinh.Text = "&Ghi chiết tính" Then 'save------------------------------------------------------

            If Me_AddEdit = "add" Then
                If Not CheckKey() Then Exit Sub
            End If

            If Not CheckInput() Then Exit Sub

            If Not SaveMaster() Then Exit Sub
            If Not SaveDetail() Then Exit Sub

            XtraMessageBox.Show("Ghi dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Status(0)
        End If

    End Sub

    Private Sub cmdSuaChietTinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSuaChietTinh.Click
        If Me.cmdSuaChietTinh.Text = "&Sửa" Then
            Status(2)

        ElseIf Me.cmdSuaChietTinh.Text = "&Bỏ qua" Then
            Status(0)

        End If
    End Sub

    Private Sub cmdThemVatTu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThemVatTu.Click
        AddDetail()
    End Sub

    Private Sub cmdXoaVatTu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXoaVatTu.Click
        Me.GridView1.DeleteSelectedRows()
    End Sub

    Private Sub cmdXoaChietTinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXoaChietTinh.Click
        If XtraMessageBox.Show("Chắc chắn xóa chiết tính?", "Quản lý chiết tính", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim myChietTinhF As New ChietTinhF
            If myChietTinhF.Delete(Me.txtSoPhieu1.EditValue.ToString.Trim + Me.txtSoPhieu2.EditValue.ToString.Trim) Then
                XtraMessageBox.Show("Xóa dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.Close()
            Else
                XtraMessageBox.Show("Xóa dữ liệu không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If
    End Sub

    Private Sub GridView1_CellValueChanged(ByVal sender As Object, ByVal e As DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs) Handles GridView1.CellValueChanged
        Me.txtSoMon.EditValue = CountSoMon()
    End Sub

    Private Sub btnXem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXem.Click
        PrintPreview_KhachHang("preview")
        PrintPreview_DienLuc("preview")
    End Sub

    Private Sub PrintPreview_KhachHang(ByVal Pri As String)
        Dim ChietTinhF As New ChietTinhF
        Dim myDtb As New DataTable

        'datasource
        myDtb = ChietTinhF.InChietTinh_KhachHang(Me.txtSoPhieu1.EditValue + Me.txtSoPhieu2.EditValue.ToString.Trim)
        If myDtb.Rows.Count = 0 Then
            'XtraMessageBox.Show("Không có dữ liệu", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Exit Sub
        End If
        Dim Report As New rptBangChietTinh_KhachHang
        Report.SetDataSource(myDtb)

        'parameter
        Dim SysF As New SysF
        myDtb = SysF.GetInfo
        If myDtb.Rows.Count = 1 Then
            Report.SetParameterValue("tendv", myDtb.Rows(0).Item("tendv"))
            Report.SetParameterValue("tendvcq", myDtb.Rows(0).Item("tendvcq"))
        Else
            Report.SetParameterValue("tendv", "")
            Report.SetParameterValue("tendvcq", "")
            XtraMessageBox.Show("Chưa nhập thông tin đơn vị", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
        End If

        'print/preview
        If Pri = "print" Then
            Report.PrintToPrinter(1, False, 1, 1)
            Report.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperFanfoldUS
        ElseIf Pri = "preview" Then
            Dim f As New frmPreview
            f.CrystalReportViewer1.ReportSource = Report
            f.WindowState = FormWindowState.Maximized
            f.ShowDialog()

        End If

        ChietTinhF.Print(Me.txtSoPhieu1.EditValue + Me.txtSoPhieu2.EditValue.ToString.Trim)


    End Sub

    Private Sub PrintPreview_DienLuc(ByVal Pri As String)

        Dim ChietTinhF As New ChietTinhF
        Dim myDtb As New DataTable

        'datasource
        myDtb = ChietTinhF.InChietTinh_DienLuc(Me.txtSoPhieu1.EditValue + Me.txtSoPhieu2.EditValue.ToString.Trim)
        If myDtb.Rows.Count = 0 Then
            XtraMessageBox.Show("Không có dữ liệu", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Exit Sub
        End If
        Dim Report As New rptBangChietTinh_DienLuc
        Report.SetDataSource(myDtb)

        'parameter
        Dim SysF As New SysF
        myDtb = SysF.GetInfo
        If myDtb.Rows.Count = 1 Then
            Report.SetParameterValue("tendv", myDtb.Rows(0).Item("tendv"))
            Report.SetParameterValue("tendvcq", myDtb.Rows(0).Item("tendvcq"))
        Else
            Report.SetParameterValue("tendv", "")
            Report.SetParameterValue("tendvcq", "")
            XtraMessageBox.Show("Chưa nhập thông tin đơn vị", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
        End If

        'print/preview
        If Pri = "print" Then
            Report.PrintToPrinter(1, False, 1, 1)
            Report.PrintOptions.PaperSize = CrystalDecisions.Shared.PaperSize.PaperFanfoldUS
        ElseIf Pri = "preview" Then
            Dim f As New frmPreview
            f.CrystalReportViewer1.ReportSource = Report
            f.WindowState = FormWindowState.Maximized
            f.ShowDialog()

        End If

        ChietTinhF.Print(Me.txtSoPhieu1.EditValue + Me.txtSoPhieu2.EditValue.ToString.Trim)

    End Sub

#Region "Focus"
    Private Sub lueLoaiChietTinh_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lueLoaiChietTinh.KeyDown
        If e.KeyCode = Keys.Enter Then Me.txtSoPhieu2.Focus()
    End Sub

    Private Sub txtSoPhieu2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSoPhieu2.KeyDown
        If e.KeyCode = Keys.Enter Then Me.txtNgayLap.Focus()
    End Sub

    Private Sub txtNgayLap_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtNgayLap.KeyDown
        If e.KeyCode = Keys.Enter Then Me.txtSo_Don.Focus()
    End Sub

    Private Sub txtSoDon_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSo_Don.KeyDown
        Dim mykhclsf As New KhachHangF
        Dim mykhdatainfo As DataTable
        If e.KeyCode = Keys.Enter Then
            If Me.txtSo_Don.EditValue.ToString.Trim = "" Or IsNumeric(Me.txtSo_Don.Text.Trim) = False Then
                XtraMessageBox.Show("Số đơn không đúng", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else

                mykhdatainfo = mykhclsf.GetInforOfKhang("PK0200", CLng(Me.txtSo_Don.Text))
                If mykhdatainfo Is Nothing Then
                    XtraMessageBox.Show("Số đơn không đúng", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    Me.txtSo_Don.Focus()
                Else
                    Me.txtKhachHang.Text = mykhdatainfo.Rows(0).Item(4)
                    Me.txtDiaChi.Text = mykhdatainfo.Rows(0).Item(5)
                    Me.lueMa_DViDChinh.EditValue = mykhdatainfo.Rows(0).Item(3)
                    If lueMa_DViDChinh.Text.Trim = "" Then
                        XtraMessageBox.Show("Nhập lại mã địa chính đúng", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
                        lueMa_DViDChinh.Focus()
                    Else
                        Me.txtVanChuyen.Focus()
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub lueMa_DViDChinh_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lueMa_DViDChinh.KeyDown
        If e.KeyCode = Keys.Enter Then
            Me.txtKhachHang.Focus()
        End If
    End Sub

    Private Sub txtKhachHang_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtKhachHang.KeyDown
        If e.KeyCode = Keys.Enter Then Me.txtDiaChi.Focus()
    End Sub

    Private Sub txtDiaChi_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDiaChi.KeyDown
        If e.KeyCode = Keys.Enter Then Me.txtVanChuyen.Focus()
    End Sub

    Private Sub txtVanChuyen_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtVanChuyen.KeyDown
        If e.KeyCode = Keys.Enter Then Me.GridView1.Focus()
    End Sub
#End Region

    Private Sub GridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GridView1.KeyDown
        If e.KeyCode = Keys.Enter Then Me.cmdThemChietTinh.Focus()
    End Sub

   
End Class